package com.wyc.servicePassengerUser.service.impl;
import java.util.Date;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wyc.internalCommon.constant.CommonStatusEnum;
import com.wyc.internalCommon.request.VerificationCodeDTO;
import com.wyc.internalCommon.result.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wyc.servicePassengerUser.mapper.PassengerUserMapper;
import com.wyc.servicePassengerUser.dto.PassengerUser;
import com.wyc.servicePassengerUser.service.PassengerUserService;

/**
 * 乘客用户Service业务层处理
 * 
 * @author li
 * @date 2023-03-21
 */
@Service
public class PassengerUserServiceImpl implements PassengerUserService
{
    @Autowired
    private PassengerUserMapper passengerUserMapper;

    /**
     * 查询乘客用户
     * 
     * @param id 乘客用户主键
     * @return 乘客用户
     */
    @Override
    public PassengerUser selectPassengerUserById(Long id)
    {
        return passengerUserMapper.selectPassengerUserById(id);
    }

    /**
     * 查询乘客用户列表
     * 
     * @param passengerUser 乘客用户
     * @return 乘客用户
     */
    @Override
    public List<PassengerUser> selectPassengerUserList(PassengerUser passengerUser)
    {
        return passengerUserMapper.selectPassengerUserList(passengerUser);
    }

    /**
     * 新增乘客用户
     * 
     * @param passengerUser 乘客用户
     * @return 结果
     */
    @Override
    public int insertPassengerUser(PassengerUser passengerUser)
    {
        return passengerUserMapper.insertPassengerUser(passengerUser);
    }

    /**
     * 修改乘客用户
     * 
     * @param passengerUser 乘客用户
     * @return 结果
     */
    @Override
    public int updatePassengerUser(PassengerUser passengerUser)
    {
        return passengerUserMapper.updatePassengerUser(passengerUser);
    }

    /**
     * 批量删除乘客用户
     * 
     * @param ids 需要删除的乘客用户主键
     * @return 结果
     */
    @Override
    public int deletePassengerUserByIds(Long[] ids)
    {
        return passengerUserMapper.deletePassengerUserByIds(ids);
    }

    /**
     * 删除乘客用户信息
     * 
     * @param id 乘客用户主键
     * @return 结果
     */
    @Override
    public int deletePassengerUserById(Long id)
    {
        return passengerUserMapper.deletePassengerUserById(id);
    }

    /**
     * 根据手机号查询是否存在该乘客，不存在新增
     * @return
     */
    @Override
    public ApiResult loginOrReg(VerificationCodeDTO verificationCodeDTO) {
        // 手机号
        String passengerPhone = verificationCodeDTO.getPassengerPhone();
        // 根据查询是否存在该乘客
        PassengerUser passengerUser = new PassengerUser();
        passengerUser.setPassengerPhone(passengerPhone);
        List<PassengerUser> userList = passengerUserMapper.selectPassengerUserList(passengerUser);
        if (userList.size() == 0){
            // 不存在该乘客  新增乘客
            passengerUser.setPassengerName("小明");
            passengerUser.setPassengerGender((byte)1);
            passengerUser.setState((byte)0);
            Date date = new Date();
            passengerUser.setGmtCreate(date);
            passengerUser.setGmtModified(date);
            passengerUserMapper.insertPassengerUser(passengerUser);
        }

        return ApiResult.success();
    }

    /**
     * 根据手机号查询乘客信息
     * @return
     */
    @Override
    public ApiResult getUserByPhone(String passengerPhone) {
        Map<String, Object> map = new HashMap<>();
        map.put("passenger_phone",passengerPhone);
        List<PassengerUser> passengerUser = passengerUserMapper.selectByMap(map);

        if (passengerUser.size() == 0 || null == passengerUser){
            return ApiResult.error(CommonStatusEnum.USER_NOT_EXISTS.getCode(),CommonStatusEnum.USER_NOT_EXISTS.getMsg());
        }

        return ApiResult.success(passengerUser.get(0));
    }
}
